package com.alcatel_lucent.osp.action;

import com.alcatel_lucent.osp.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Author: Allan YANG
 * Date: 2008-11-13
 */
@SuppressWarnings("serial")
public class UserContext implements Serializable {

    public static String SESSION_USER_CONTEXT = "user_context";
    private static ThreadLocal<UserContext> instance = new ThreadLocal<UserContext>();

    private static Logger log = LoggerFactory.getLogger(UserContext.class);

    private Locale locale;
    private User user;
    private TimeZone timeZone;

    public UserContext() {
    }

    public UserContext(Locale locale, User user,TimeZone timeZone) {
        this.locale = locale;
        this.user = user;
        this.timeZone = timeZone;
    }

    public TimeZone getTimeZone() {
        return timeZone;
    }

    public void setTimeZone(TimeZone timeZone) {
        this.timeZone = timeZone;
    }

    public static UserContext getInstance() {
        return instance.get();
    }

    public static void setUserContext(UserContext uc) {
//        log.info("Set userContext " + uc + "in to instance, thread name " + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() + ")");
        instance.set(uc);
    }

    public static void removeUserContext() {
//        log.info("Remove userContext " + instance.get() + "from instance, thread name " + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() + ")");
        instance.remove();
    }

    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "UserContext{" +
                "user=" + user +
                ", locale=" + locale +
                '}';
    }
}
